<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>工作调度-审批</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
    <style>
        .layui-disabled, .layui-disabled:hover {
            color: #000000 !important;
            cursor: not-allowed !important;
        }

        .layui-form-label {
            width: 100px;
        }

        .layui-input-block {
            margin-left: 130px;
        }

        .layui-input layui-unselect {
            cursor: pointer;
        }

        .layui-table {
            width: 98%;
        }

        #content1, #ddmc, #createName, #contentdate, .layui-unselect {
            border: none;
            outline: none; /* 去掉聚焦时的边框 */
            background: transparent; /* 可选，去掉背景 */
        }

        button:disabled {
            background-color: #d8d8d8 !important; /* 让按钮背景变灰 */
            color: #a0a0a0 !important; /* 文字颜色变浅 */
            cursor: not-allowed; /* 鼠标变成禁止符号 */
        }

    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <!--填写调度内容-->
            <div id="ajTable" class="layui-card" style="display:none;">
                <div class="layui-card-body">
                    <form class="layui-form" lay-filter="ddForm">
                        <label for="dduuid"></label>
                        <input type="text" id="dduuid" class="layui-input" style="display: none">
                        <label for="type"></label>
                        <input type="text" id="type" class="layui-input" style="display: none">
                        <label for="mold"></label>
                        <input type="text" id="mold" class="layui-input" style="display: none">
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                            <legend>
                                <label for="ddmc"></label>
                                <input type="text" id="ddmc" style="width: 300px" class="layui-input">
                            </legend>
                        </fieldset>
                        <table id="ajxx_table" class="layui-table" lay-filter="ajxx_table"></table>
                        <div class="layui-form-item" style="display: flex; justify-content: center;">
                            <!--                <button class="layui-btn" id="submitGzdd" type="button">提交</button>-->
                            <button class="layui-btn layui-btn-primary" id="btn_cancel" type="button">返回</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="layui-col-md12">
            <!--主页表格-->
            <div id="table_div" class="layui-card">
                <div class="layui-card-body">
                    <!--待完成-->
                    <div class="layui-form-item"></div>
                    <table class="layui-table" id="waitShowTable" lay-filter="waitShowTable"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md12">
            <!--填报工作调度-->
            <div id="tbGzddForm" style="display: none">
                <div id="content2and3Div" style="margin-top: 10px"></div>
            </div>
        </div>
    </div>
</div>


<!--填报-->
<script type="text/html" id="tool2">
    <a class="layui-btn layui-btn-xs" lay-event="tbGzdd">填写</a>
</script>

<script type="text/html" id="waittool">
    {{# if(d.type == "4"){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">填写</a>
    {{# } else if(d.type == "5") { }}
    <a class="layui-btn layui-btn-xs" lay-event="view">查看</a>
    {{# } }}
</script>

<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script th:inline="none">
    // 获取token
    var token = CoreUtil.getData("access_token");
    // 地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var table = layui.table;
    var waitShowTable;
    var ajxx_table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var element = layui.element;
    var maxlength = 500;
    var inspectionMan = "";
    var contentdate = null;
    var tbGzddForm = jQuery("#tbGzddForm")
    var _surveyDetail;
    layui.use(['table', 'layer', 'laydate', 'upload', 'element'], function () {
        // 调度列表
        waitShowTable = table.render({
            elem: '#waitShowTable',
            contentType: 'application/json',
            headers: {
                "authorization": token
            },
            page: true, // 开启分页
            url: '/workflow/log/queryGzddContent',//数据接口
            method: 'POST',
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null
                        : CoreUtil.paramsDecrypt(res.data.records)
                }
            },
            cols: [[
                {field: 'id', title: 'id', hide: true},
                {field: 'mold', title: 'mold', hide: true},
                {field: 'dduuid', title: 'dduuid', hide: true},
                {field: 'createName', title: '创建人', align: 'center', width: 200, sort: true},
                {
                    field: 'contentdate',
                    title: '创建日期',
                    align: 'center',
                    width: 120,
                    sort: true,
                    templet: function (d) {
                        if (!d.contentdate) {
                            return '';
                        }
                        return layui.util.toDateString(d.contentdate, 'yyyy-MM-dd');
                    }
                },
                {field: 'ddmc', title: '调度名称', align: 'center', width: 300, sort: true},
                {field: 'content1', title: '调度模板', align: 'center', width: 300, sort: true},
                {field: 'tbzt', title: '填报状态', align: 'center', width: 220, sort: true},
                {
                    align: 'center',
                    width: 140,
                    field: 'type',
                    title: '调度状态',
                    sort: true,
                    templet: function (d) {
                        // 根据 type 的值返回对应的状态文本
                        if (d.type === '3' || d.type === '7') {
                            return '<span>待调度</span>'; // 橙色
                        } else if (d.type === '4') {
                            return '<span>调度中</span>'; // 绿色
                        } else if (d.type === '5') {
                            return '<span>已完成</span>'; // 蓝色
                        } else if (d.type === '6') {
                            return '<span>失效</span>'; // 红色
                        } else {
                            return '<span>未知状态</span>'; // 默认
                        }
                    }
                },
                {
                    field: 'updateTime', title: '完成日期', align: 'center', width: 120, sort: true,
                    templet: function (d) {
                        if (!d.updateTime) {
                            return '';
                        }
                        return layui.util.toDateString(d.updateTime, 'yyyy-MM-dd');
                    }
                },
                {fixed: 'right', align: 'center', width: 100, toolbar: "#waittool", title: '操作'}
            ]],
            toolbar: '#toolbar'
        });
        // 调度列操作
        table.on('tool(waitShowTable)', function (obj) {
            let event = obj.event;
            let data = obj.data;
            switch (event) {
                case "view":
                case "edit":
                    console.log("调度内容：", data)
                    $("#table_div").hide();
                    $("#ajTable").show();
                    //主键
                    $("#dduuid").val(data.id);
                    //调度名称
                    $("#ddmc").val(data.ddmc);
                    //调度状态
                    $("#type").val(data.type);
                    //调度模板ID
                    $("#mold").val(data.mold);
                    // 案件列表
                    ajxx_table = table.render({
                        elem: '#ajxx_table',
                        contentType: 'application/json',
                        headers: {
                            "authorization": token
                        },
                        where: {
                            "id": data.id,
                            "jcajxxuuids": data.jcajxxuuids
                        },
                        page: true, // 开启分页
                        url: "/workflow/log/queryGzddGrAjTables",//数据接口
                        method: 'POST',
                        parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                            return {
                                "code": res.code, //解析接口状态
                                "msg": res.msg, //解析提示文本
                                "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                                "data": CoreUtil.isEmpty(res.data) ? null
                                    : CoreUtil.paramsDecrypt(res.data.records)
                            }
                        },
                        cols: [[{type: 'checkbox', fixed: 'left'},
                            {
                                align: 'center',
                                width: 50,
                                field: 'jcajxxuuid',
                                title: '检查UUID',
                                hide: true
                            }, {
                                align: 'center',
                                width: 200,
                                field: 'jcajbh',
                                title: '检查案件编号',
                                sort: true
                            }, {
                                align: 'center',
                                width: 320,
                                field: 'ajmc',
                                title: '案件名称',
                                sort: true
                            }, {
                                align: 'center',
                                width: 120,
                                field: 'ajzt',
                                title: '案件状态',
                                sort: true
                            }, {
                                align: 'center',
                                width: 200,
                                field: 'nsrsbh',
                                title: '纳税人识别号',
                                sort: true
                            }, {
                                align: 'center',
                                width: 200,
                                field: 'nsrmc',
                                title: '纳税人名称',
                                sort: true
                            }, {
                                align: 'center',
                                width: 200,
                                field: 'ddjsry',
                                title: '调度接收人员',
                                sort: true
                            }, {
                                align: 'center',
                                width: 160,
                                field: 'ddzt',
                                title: '填报状态',
                                sort: true
                            }, {
                                align: 'center',
                                width: 320,
                                field: 'ajContent',
                                title: '调度内容',
                                hide: true
                            }, {
                                align: 'center',
                                width: 320,
                                field: 'ajContentId',
                                title: '调度内容id',
                                hide: true
                            }, {
                                fixed: 'right',
                                align: 'center',
                                width: 180,
                                toolbar: "#tool2",
                                title: '操作',
                                sort: true
                            }
                        ]], toolbar: '#toolbar'
                    });
                    break;
                default:
                    // 其他情况（可选）
                    break;
            }
        });
        //下派、填报、删除
        table.on('tool(ajxx_table)', function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case 'tbGzdd':
                    let content2and3Div = $("#content2and3Div");
                    //判断是否为修改
                    if (data.ajcontent !== null && data.ajcontent !== undefined && data.ajcontent !== "") {
                        console.log("进入到修改页面")
                        CoreUtil.sendPost("/workflow/log/getGzddTbView", data, function (res) {
                            if (res.code === 0) {
                                content2and3Div.html("");
                                let _data = res.data;
                                _surveyDetail = _data;
                                let viewSurveyTmp = "";
                                for (let _i in _data) {
                                    let _optionsConent = _data[_i].optionsFeedback;
                                    let _optionsTitle = _data[_i].optionsTitle;
                                    let _optionsType = _data[_i].optionsType;
                                    let _id = _data[_i].optionsId;
                                    if (_optionsType === "1") {
                                        // 文本
                                        viewSurveyTmp += "<div class='layui-form-item'>"
                                            + "<label style='' class='layui-form-label'>" + _optionsTitle + "</label>"
                                            + "<div class='layui-input-block'>"
                                            + "<input type='text' class='layui-input " + _id + "' value='" + _optionsConent + "'>"
                                            + "</div>"
                                            + "<hr>";
                                    } else if (_optionsType === "2") {
                                        // 多选（保持第一段的分层逻辑）
                                        let _contentSurveyJson = eval("(" + _optionsConent + ")");
                                        let l = Object.keys(_contentSurveyJson).length;
                                        viewSurveyTmp += "<div class='layui-form-item'>"
                                            + "<label style='height: " + l * 60 + "px' class='layui-form-label'>" + _optionsTitle + "</label>";

                                        for (let _ii in _contentSurveyJson) {
                                            let option = _contentSurveyJson[_ii];
                                            let checkedClass = option.selected ? "layui-form-checked" : "";

                                            viewSurveyTmp += "<div onclick='optionsDuoxuan(this," + _ii + ")' class='layui-unselect layui-form-checkbox " + checkedClass + " " + _id + "' lay-skin='primary'>"
                                                + "<div></div>"
                                                + "<i class='layui-icon layui-icon-ok'></i>"
                                                + "</div>" + option.uuid;

                                            if (!CoreUtil.isEmpty(option.extraText)) {
                                                viewSurveyTmp += "<input style='width: 40%' type='text' class='layui-input " + _id + "_" + _ii + "' value='" + option.extraText + "'>";
                                            }
                                            viewSurveyTmp += "<br>";
                                        }
                                        viewSurveyTmp += "<hr>";
                                    } else if (_optionsType === "3") {
                                        // 单选（对齐第一段的DOM结构）
                                        let _contentSurveyJson = eval("(" + _optionsConent + ")");
                                        let l = Object.keys(_contentSurveyJson).length;
                                        viewSurveyTmp += "<div class='layui-form-item'>"
                                            + "<label style='height: " + l * 60 + "px' class='layui-form-label'>" + _optionsTitle + "</label>";

                                        for (let _ii in _contentSurveyJson) {
                                            let option = _contentSurveyJson[_ii];
                                            let checkedClass = option.selected ? "layui-form-radioed" : "";

                                            viewSurveyTmp += "<div onclick='optionsDanxuan(this," + _ii + ")' class='layui-unselect layui-form-radio " + checkedClass + " " + _id + "'>"
                                                + "<i class='layui-anim layui-icon layui-icon-radio'></i>"
                                                + "<div>" + option.uuid + "</div>"
                                                + "</div>";

                                            if (option.extraText) {
                                                viewSurveyTmp += "<input style='width: 40%' type='text' class='layui-input " + _id + "_" + _ii + "' value='" + option.extraText + "'>";
                                            }
                                            viewSurveyTmp += "<br>";
                                        }
                                        viewSurveyTmp += "<hr>";
                                    }
                                }
                                content2and3Div.html(viewSurveyTmp);
                                //日期
                                laydate.render({
                                    elem: '.layui-date'
                                });
                            } else {
                                layer.msg(res.msg);
                            }
                        });
                    } else {
                        console.log("进入到第一次加载页面")
                        let mapArr = {
                            id: $("#mold").val()
                        }
                        CoreUtil.sendPost("/workflow/log/survey/getGzddContentById", mapArr, function (res) {
                            if (res.code === 0) {
                                content2and3Div.html("");
                                let _data = res.data;
                                _surveyDetail = _data;
                                let viewSurveyTmp = "";
                                for (let _i in _data) {
                                    let _optionsConent = _data[_i].optionsConent;
                                    let _contentSurveyJson = eval("(" + _optionsConent + ")");
                                    let _optionsTitle = _data[_i].optionsTitle;
                                    let _optionsType = _data[_i].optionsType;
                                    let _id = _data[_i].id;
                                    let _surveyId = _data[_i].surveyId;
                                    if (_optionsType == "1") {
                                        // 文本
                                        viewSurveyTmp += "<div class='layui-form-item'>"
                                            + "<label style='' class='layui-form-label'>" + _optionsTitle + "</label>"
                                            + "<div class='layui-input-block'>";
                                        if (_contentSurveyJson[0] == "date") {
                                            viewSurveyTmp += "<input type='text' name='date' class='layui-date layui-input " + _id + "' placeholder='请输入yyyy-mm-dd格式'>";
                                        } else {
                                            viewSurveyTmp += "<input type='text' class='layui-input " + _id + "' placeholder='请输入'>";
                                        }
                                        viewSurveyTmp += "</div><br>";
                                    } else if (_optionsType == "2") {
                                        let l = Object.keys(_contentSurveyJson).length;
                                        // 多选
                                        viewSurveyTmp += "<div class='layui-form-item'>"
                                            + "<label style='height: " + l * 60 + "px' class='layui-form-label'>" + _optionsTitle + "</label>";
                                        for (let _ii in _contentSurveyJson) {
                                            viewSurveyTmp += "<div onclick='optionsDuoxuan(this," + _ii + ")' class='layui-unselect layui-form-checkbox " + _id + "' lay-skin='primary'><div></div><i class='layui-icon layui-icon-ok'></i></div>" + _contentSurveyJson[_ii][0];
                                            let flag = _contentSurveyJson[_ii][1];
                                            if (flag) {
                                                viewSurveyTmp += "<input style='width: 40%' type='text' class='layui-input " + _id + "_" + _ii + "' placeholder='请输入'>";
                                            }
                                            viewSurveyTmp += "<br>";
                                        }
                                        viewSurveyTmp += "<hr>";
                                    } else if (_optionsType == "3") {
                                        let l = Object.keys(_contentSurveyJson).length;
                                        // 单选
                                        viewSurveyTmp += "<div class='layui-form-item'>"
                                            + "<label style='height: " + l * 60 + "px' class='layui-form-label'>" + _optionsTitle + "</label>";
                                        for (let _ii in _contentSurveyJson) {
                                            /* viewSurveyTmp += "<div class='layui-form'>"
                                                          // + "<label>"+_contentSurveyJson[_ii][0]+"</label>"
                                                          + "<input class='"+_id+"' name='radio1_"+_id+"' value='"+_contentSurveyJson[_ii][0]+"' type='radio'>"
                                                          + "</div>"; */
                                            viewSurveyTmp += "<div onclick='optionsDanxuan(this," + _ii + ")' class='layui-unselect layui-form-radio " + _id + "'><i class='layui-anim layui-icon layui-icon-radio'></i><div>" + _contentSurveyJson[_ii][0] + "</div></div>";
                                            let flag = _contentSurveyJson[_ii][1];
                                            if (flag) {
                                                viewSurveyTmp += "<input style='width: 40%' type='text' class='layui-input " + _id + "_" + _ii + "' placeholder='请输入'>";
                                            }
                                            viewSurveyTmp += "<br>";
                                        }
                                        viewSurveyTmp += "<hr>";
                                    }
                                }
                                content2and3Div.html(viewSurveyTmp);
                                //日期
                                laydate.render({
                                    elem: '.layui-date'
                                });
                            } else {
                                layer.msg(res.msg);
                                return false;
                            }
                        });
                    }
                    // 在此处输入 layer 的任意代码
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-molv',
                        title: "填报调度",
                        area: ['60%', '500px'],
                        shade: 0.6, // 遮罩透明度
                        maxmin: true, // 允许全屏最小化
                        shadeClose: true,
                        content: tbGzddForm,
                        btn: ['确定', '取消'],
                        btnAlign: 'c',  // 让按钮居中
                        yes: function (index) {
                            let feelBackArr = [];
                            for (var _i in _surveyDetail) {
                                var _optionsTitle = _surveyDetail[_i].optionsTitle;
                                let _optionsConent = _surveyDetail[_i].optionsConent || "{}"; // 空值兜底
                                _surveyDetail[_i].optionsConent = _optionsConent.trim() === "" ? "{}" : _optionsConent;
                                var _optionsType = _surveyDetail[_i].optionsType;
                                var _id = _surveyDetail[_i].id;
                                var _surveyId = _surveyDetail[_i].surveyId;
                                if (_optionsType == "1") {
                                    // 文本
                                    var uuid = $("." + _id + "").val();
                                    if (CoreUtil.isEmpty(uuid)) {
                                        layer.msg(_optionsTitle + "未填写，请填写！");
                                        return false;
                                    }
                                    var map = {
                                        surveyId: _surveyId,
                                        optionsId: _id,
                                        optionsType: _optionsType,
                                        optionsTitle: _optionsTitle,
                                        feelBack: uuid
                                    }
                                    feelBackArr.push(map);
                                } else if (_optionsType == "2") {
                                    // 多选
                                    var _checkedArray = [];
                                    var _valueArray = [];
                                    for (var _ii in _contentSurveyJson) {
                                        var _className = $("." + _id + "")[_ii].className;
                                        if (_className.indexOf("layui-form-checked") != -1) {
                                            var uuid = _contentSurveyJson[_ii][0];
                                            var flag = _contentSurveyJson[_ii][1];
                                            if (flag) {
                                                var _text = $("." + _id + "_" + _ii).val();
                                                if (CoreUtil.isEmpty(_text)) {
                                                    layer.msg(_optionsTitle + "," + _contentSurveyJson[_ii][0] + "未选择后，未填写！");
                                                    return false;
                                                } else {
                                                    uuid += "_" + _text;
                                                }
                                            }
                                            _valueArray.push(uuid);
                                        }

                                    }
                                    var map = {
                                        surveyId: _surveyId,
                                        optionsId: _id,
                                        optionsType: _optionsType,
                                        optionsTitle: _optionsTitle,
                                        feelBack: _valueArray
                                    }
                                    feelBackArr.push(map);
                                    if (_valueArray.length == 0) {
                                        layer.msg(_optionsTitle + "未选择，请选择！");
                                        return false;
                                    }
                                } else if (_optionsType == "3") {
                                    // 单选
                                    var _length = $("." + _id).length;
                                    var uuid = "";
                                    for (_i = 0; _i < _length; _i++) {
                                        var _className = $("." + _id)[_i].className;
                                        if (_className.indexOf("layui-form-radioed") != -1) {
                                            uuid += _contentSurveyJson[_i][0];
                                            var flag = _contentSurveyJson[_i][1];
                                            if (flag) {
                                                var _text = $("." + _id + "_" + _i).val();
                                                if (CoreUtil.isEmpty(_text)) {
                                                    layer.msg(_optionsTitle + "," + _contentSurveyJson[_i][0] + "未选择后，未填写！");
                                                    return false;
                                                } else {
                                                    uuid += "_" + _text;
                                                }
                                            }
                                        }
                                    }

                                    if (CoreUtil.isEmpty(uuid)) {
                                        layer.msg(_optionsTitle + ",未选择，未填写！");
                                        return false;
                                    }
                                    var map = {
                                        surveyId: _surveyId,
                                        optionsId: _id,
                                        optionsType: _optionsType,
                                        optionsTitle: _optionsTitle,
                                        feelBack: uuid
                                    }
                                    feelBackArr.push(map);
                                }
                            }
                            let feelBack = JSON.stringify(feelBackArr)
                            //传递
                            let mapArr = {
                                //调度主键
                                dduuid: data.dduuid,
                                //调度模板-主键
                                mold: $("#mold").val(),
                                //案件信息UUID
                                jcajxxuuid: data.jcajxxuuid,
                                // 案件ID
                                ajContentId: data.ajContentId,
                                // 填报内容
                                ajcontent: feelBack,
                            }
                            CoreUtil.sendPost("/workflow/log/tbGzdd", mapArr, function (res) {
                                if (res.code === 0) {
                                    layer.msg("填报调度成功！", {icon: 1});
                                    layer.close(index);
                                    AjSearch();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                    return false;
                                }
                            });
                        }
                    });
                    break;
            }
        });
        // 返回
        $("#btn_cancel").click(function () {
            back();
            return false;
        });
    });

    // 执行查询
    function waitSearch() {
        waitShowTable.reload({
            where: {},
            page: {
                curr: 1
            }
        });
    }


    // 执行查询
    function AjSearch() {
        ajxx_table.reload({
            where: {},
            page: {
                curr: 1
            }
        });
    }

    function back() {
        waitSearch();
        $("#table_div").show();
        $("#ajTable").hide();
    }

    //多选
    function optionsDuoxuan(that, i) {
        let $checkbox = $(that);
        if ($checkbox.hasClass("layui-form-checked")) {
            // 取消选中
            $checkbox.removeClass("layui-form-checked");
        } else {
            // 选中
            $checkbox.addClass("layui-form-checked");
        }
        console.log(that);
    }

    // 单选方法
    function optionsDanxuan(that, i) {
        let className = $(that)[0].className;
        console.log("当前元素的 className:", className); // 调试信息
        // 拆分类名并过滤掉空字符串
        let classArr = className.split(' ').filter(cls => cls.trim() !== "");
        console.log("拆分后的 classArr:", classArr); // 调试信息
        if (classArr.length < 3) {
            console.error("类名数量不足，无法获取唯一标识符");
            return;
        }
        let targetClass = classArr[2]; // 假设第三个类是唯一标识符
        console.log("目标类名:", targetClass); // 调试信息
        if (!targetClass || targetClass.indexOf(".") !== -1 || targetClass.indexOf(" ") !== -1) {
            console.error("目标类名无效:", targetClass);
            return;
        }
        if (className.indexOf("layui-form-radioed") !== -1) {
            // 如果已经选中，则不处理
        } else {
            // 更新当前选项的 UI 状态
            $(that)[0].className = classArr[0] + ' ' + classArr[1] + ' ' + targetClass + " layui-form-radioed";
            // 更新其他选项的 UI 状态
            let length = $("." + targetClass).length;
            for (let _i = 0; _i < length; _i++) {
                if (_i !== i) {
                    $("." + targetClass)[_i].className = classArr[0] + ' ' + classArr[1] + ' ' + targetClass;
                }
            }
        }
    }

</script>

</body>
</html>